﻿using System.Linq;
using System.Windows.Navigation;
using CuteRibs.MVVMLib;
using CuteRibs.WowArmory.Models;
using CuteRibs.WowArmory.Resources;
using System.Windows.Media;
using System;

namespace CuteRibs.WowArmory.ViewModels
{
	public class CharacterPageViewModel : PageViewModelBase
	{
		public AppRes AppRes { get { return App.AppResource; } }
		public AppTitleControlViewModel AppTitle { get; set; }

		private CharacterDetail character;
		private string avatar;
		private SolidColorBrush classColor;
		private string raceName;
		private string className;

		public CharacterDetail Character
		{
			get { return this.character; }
			set
			{
				if (this.character != value)
				{
					this.character = value;
					this.NotifyPropertyChanged("Character");
				}
			}
		}

		public SolidColorBrush ClassColor
		{
			get { return this.classColor; }
			set
			{
				if (this.classColor != value)
				{
					this.classColor = value;
					this.NotifyPropertyChanged("ClassColor");
				}
			}
		}

		public string RaceName
		{
			get { return this.raceName; }
			set
			{
				if (this.raceName != value)
				{
					this.raceName = value;
					this.NotifyPropertyChanged("RaceName");
				}
			}
		}

		public string ClassName
		{
			get { return this.className; }
			set
			{
				if (this.className != value)
				{
					this.className = value;
					this.NotifyPropertyChanged("ClassName");
				}
			}
		}

		public string Avatar
		{
			get { return this.avatar; }
			set
			{
				if (this.avatar != value)
				{
					this.avatar = value;
					this.NotifyPropertyChanged("Avatar");
				}
			}
		}

		public CharacterPageViewModel()
		{
			if (App.IsDesignMode)
			{
				this.SetCharacter(null, null);
			}
		}

		public override void OnNavigatedTo(ViewBase sender, NavigationEventArgs eventArgs, NavigationContext context)
		{
			base.OnNavigatedTo(sender, eventArgs, context);

			this.SetCharacter(context.QueryString["Realm"], context.QueryString["Character"]);
		}

		private void SetCharacter(string realm, string charName)
		{
			Region region;

			if (App.IsDesignMode)
			{
				region = Region.Americas;
			}

			region = App.RegionData.Region;
			this.AppTitle = new AppTitleControlViewModel(AppRes.Title_CharacterDetails, region.DisplayName);
			this.SetCharacter(region, realm, charName, CharacterField.Guild, CharacterField.Stats, CharacterField.Talents, CharacterField.Items);
		}

		private async void SetCharacter(Region region, string realmName, string charName, params string[] fields)
		{
			this.ShowProgressBar = true;

			try
			{
				var character = await App.GetCharacter(region, realmName, charName, fields);
				var raceResource = await App.LoadRaceResource(region);
				var classResource = await App.LoadClassResource(region);

				if (character == null)
				{
					App.ShowAlert(string.Format(AppRes.MsgCannotFindCharacter, charName, region.DisplayName, realmName));
					this.ShowProgressBar = false;
					return;
				}

				this.Character = character;
				this.Avatar = WowClient.GetAvatar(region, this.Character);
				this.ClassColor = App.GetClassColor(this.Character.Class.ToString());
				this.RaceName = raceResource.Races.First(r => r.Id == this.Character.Race).Name;
				this.ClassName = classResource.Classes.First(c => c.Id == this.Character.Class).Name;
			}
			catch (Exception ex)
			{
				App.ShowError(AppException.CreateCharacterException(ex, region.Domain, realmName, charName));
			}

			this.ShowProgressBar = false;
		}
	}
}
